package com.example.my_rabbitmq_high_demo.returnlistener;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: Return Listener模式生产者
 * @Author: GaoJian
 * @Date: 2022/7/6 23:39
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建ConnectionFactory并设置属性
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/");

        //2、创建连接
        Connection connection = factory.newConnection();

        //3、创建channel
        Channel channel = connection.createChannel();

        //4、设置Return Listener监听
        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("-----Return Listener-----");
                System.out.println(replyCode);
                System.out.println(replyText);
                System.out.println(exchange);
                System.out.println(routingKey);
                System.out.println(properties);
                System.out.println(new String(body));
            }
        });

        String exchangeName = "test_return_exchange";
        String routingKey = "return.qiye";
        String routingErrorKey = "error.qiye";

        //5、发送消息
        String msg = "Send Msg And Return Listener By RoutingKey：";
        channel.basicPublish(exchangeName, routingKey, true, null, (msg + routingKey).getBytes());
        channel.basicPublish(exchangeName, routingErrorKey, true, null, (msg + routingErrorKey).getBytes());
    }
}
